package com.shsh.safemanage.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.DepartmentDao;
import com.shsh.safemanage.dao.EquipmentDao;
import com.shsh.safemanage.dao.ProblemDao;
import com.shsh.safemanage.dao.TitlePostManageGroupDao;
import com.shsh.safemanage.dao.UserDao;
import com.shsh.safemanage.model.Department1;
import com.shsh.safemanage.model.Department2;
import com.shsh.safemanage.model.Equipment;
import com.shsh.safemanage.model.JobPost;
import com.shsh.safemanage.model.Problem;
import com.shsh.safemanage.model.ProblemTransfer;
import com.shsh.safemanage.model.User;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.Util;

public class WriteProblemAction {
	public boolean chooseManageGroupUser(HttpServletRequest request) {
		String userIDsStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String manageGroupIDStr = request.getParameter(Constants.REQUEST_PARAMETER_MANAGEGROUP_ID);

		if (userIDsStr == null || manageGroupIDStr == null) {
			return false;
		}

		if (manageGroupIDStr.equals("")) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_USER_HAS_REGISTERED);
			return false;
		}

		HttpSession session = request.getSession();

		String[] userIDArr = userIDsStr.split(",");
		Long manageGroupID = 0L;
		try {
			Map<Long, Set<Long>> map = (Map<Long, Set<Long>>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_USER);
			if (map == null) {
				map = new HashMap<Long, Set<Long>>();
				session.setAttribute(Constants.SESSION_MANAGE_GROUP_USER, map);
			}

			manageGroupID = new Long(manageGroupIDStr);
			Set<Long> set = map.get(manageGroupID);
			if (set == null) {
				set = new HashSet<Long>();
				map.put(manageGroupID, set);
			}

			for (String userIDStr : userIDArr) {
				Long userID = new Long(userIDStr);
				set.add(userID);
			}

			return true;
		} catch (Exception e) {
		}

		return false;
	}

	public boolean chooseProblemDepartment1(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			Department1 department1 = departmentDao.getDepartment1ByID(id);

			session.setAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1, department1);

			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public boolean chooseDepartment2(HttpServletRequest request) {
		HttpSession session = request.getSession();
		
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
			Department2 department2 = departmentDao.getDepartment2ByID(id);
			
			session.setAttribute(Constants.SESSION_DEPARTMENT2, department2);
			
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public String getSessionDepartment2Str(HttpSession session) {
		Department2 depart2 = (Department2) session.getAttribute(Constants.SESSION_DEPARTMENT2);
		if (depart2 != null) {
			return depart2.getDepartment2Name();
		}
		
		return "";
	}

	public boolean chooseProblemEquipment(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		try {
			Long id = new Long(idStr);
			EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
			Equipment equipment = equipmentDao.getEquipment(id);

			session.setAttribute(Constants.SESSION_PROBLEM_EQUIPMENT, equipment);

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean writeProblem(HttpServletRequest request) {
		String problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
		String problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
		String problemZhenggai = request.getParameter(Constants.REQUEST_PARAMETER_ZHENGGAI_CUOSHI);

		HttpSession session = request.getSession();
		User user = (User) session.getAttribute(Constants.SESSION_USER);
		Department1 problemDepart1 = (Department1) session.getAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);
		Equipment problemEquip = (Equipment) session.getAttribute(Constants.SESSION_PROBLEM_EQUIPMENT);

		EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
		if (problemEquip == null) {
			List<Equipment> equipList = equipmentDao.getUserMappingEquipmentList(user.getId(), false);
			if (equipList.size() > 0) {
				problemEquip = equipList.get(0);
			}
		}
		if (problemEquip == null) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_NULL_EQUIPMENT);
			return false;
		}

		List<User> informedUserList = equipmentDao.getEquipmentMappingUser(problemEquip.getId());
		List<Long> informedUserIDList = new ArrayList<Long>();
		for (User informedUser : informedUserList) {
			informedUserIDList.add(informedUser.getId());
		}

		Long problemDepart1ID = null;
		if (problemDepart1 == null) {
			problemDepart1ID = user.getDepartment1ID();
		} else {
			problemDepart1ID = problemDepart1.getId();
		}

		Problem problem = new Problem();
		problem.setDepartment1ID(problemDepart1ID);
		problem.setEquipmentID(problemEquip.getId());
		problem.setProblemContent(Util.isoToUTF8(problemContent));
		problem.setProblemTitle(Util.isoToUTF8(problemTitle));
		problem.setProblemZhenggai(Util.isoToUTF8(problemZhenggai));
		problem.setProcessStatus(Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG);
		problem.setWriteTime(Calendar.getInstance());
		problem.setWriteUserID(user.getId());

		ProblemDao problemDao = DaoCreator.createProblemDao();
		problemDao.addProblem(problem);

		// transfer to users
		addTransferInManageGroups(session, user, problem, informedUserIDList, false);

		return true;
	}

	public boolean feedbackProblem(HttpServletRequest request) {
		HttpSession session = request.getSession();

		String processMethodStr = request.getParameter(Constants.REQUEST_PARAMETER_PROCESS_METHOD);
		String problemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		String feedback = request.getParameter(Constants.REQUEST_PARAMETER_FEEDBACK);
		String transferIDStr = request.getParameter(Constants.REQUEST_PARAMETER_TRANSFER_ID);

		if (feedback != null) {
			feedback = feedback.trim();
			feedback = Util.isoToUTF8(feedback);
		}

		int processMethod = 0;
		try {
			processMethod = Integer.parseInt(processMethodStr);
		} catch (Exception e) {
		}

		Long problemID = 0L;
		try {
			problemID = new Long(problemIDStr);
		} catch (Exception e) {
		}

		Long transferID = 0L;
		try {
			transferID = new Long(transferIDStr);
		} catch (Exception e) {
		}
		
		// check where user has right to process this problem
		User user = (User) session.getAttribute(Constants.SESSION_USER);
		ProblemDao problemDao = DaoCreator.createProblemDao();
		EquipmentDao equipmentDao = DaoCreator.createEquipmentDao();
		DepartmentDao departmentDao = DaoCreator.createDepartmentDao();

		Problem problem = problemDao.getProblemByID(problemID);
		ProblemTransfer problemTransfer = problemDao.getProblemTransferByID(transferID);

		TitlePostManageGroupDao postDao = DaoCreator.createTitlePostManageGroupDao();
		JobPost post1 = postDao.getJobPostByID(user.getJobPost1ID());
		JobPost post2 = postDao.getJobPostByID(user.getJobPost2ID());
		JobPost post3 = postDao.getJobPostByID(user.getJobPost3ID());

		boolean hasRight = this.hasRightToFeedback(problem, user, transferID, post1, post2, post3);
		if (!hasRight) {
			request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_HAS_NOT_RIGHT_TO_PROCESS);
			return false;
		}

		// write feedback
		int intRole = this.calRole(post1, post2, post3);
		if (processMethod == Constants.PROCESS_METHOD_FEEDBACK) {
			if ((intRole & Constants.ROLE_TYPE_ZUYUAN) > 0) {
				boolean isMainProcessor = problemDao.isMainProblemProcessor(user.getId(), problem.getId(), transferID);
				problemDao.processProblem(feedback, transferID, problemID, user.getId(), Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG);
				if (isMainProcessor) {
					List<ProblemTransfer> transferList = new ArrayList<ProblemTransfer>(1);
					ProblemTransfer transfer = new ProblemTransfer();
					transfer.setUserID(user.getId());
					transfer.setTransferedUserID(problemTransfer.getUserID());
					transfer.setProcessed(false);
					transfer.setProblemID(problem.getId());
					transfer.setMainTransferUser(true);
					transferList.add(transfer);
					problemDao.addProblemTransfers(transferList);
				}
			} else if ((intRole & Constants.ROLE_TYPE_KEYUAN) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(), Constants.PROBLEM_STATUS_FUKEZHANG);
				
				List<User> kezhangs = departmentDao.getUserIDsByDepartment2IDandRoleType(user.getDepartment2ID(), 
						Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG);
				List<ProblemTransfer> transferList = new ArrayList<ProblemTransfer>(2);
				for (User kezhang : kezhangs) {
					ProblemTransfer transfer = new ProblemTransfer();
					transfer.setUserID(user.getId());
					transfer.setTransferedUserID(kezhang.getId());
					transfer.setProcessed(false);
					transfer.setProblemID(problem.getId());
					transfer.setMainTransferUser(true);
					transferList.add(transfer);
				}
				problemDao.addProblemTransfers(transferList);
			} else {
				request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_PROCESS_METHOD_ACQUIRED);
				return false;
			}
		} else if (processMethod == Constants.PROCESS_METHOD_CLOSE) {
			problemDao.processProblem(feedback, transferID, problemID, user.getId(), Constants.PROBLEM_STATUS_CLOSE);
		} else if (processMethod == Constants.PROCESS_METHOD_TRANSFER) {
			if ((intRole & Constants.ROLE_TYPE_ZUZHANG) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_ZUYUAN);

				// transfer to zuyuan
				this.addTransferInManageGroups(session, user, problem, new ArrayList<Long>(), true);
			} else if ((intRole & Constants.ROLE_TYPE_ZHUANGZHIZHANG) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG);

				// transfer to zuzhang, zuyuan
				this.addTransferInManageGroups(session, user, problem, new ArrayList<Long>(), true);
			} else if ((intRole & (Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG)) > 0) {
				session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
				
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_FUKEZHANG);
				
				Department2 depart2 = (Department2) session.getAttribute(Constants.SESSION_DEPARTMENT2);
				List<User> kezhangs = departmentDao.getUserIDsByDepartment2IDandRoleType(depart2.getId(), Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG);
				List<ProblemTransfer> transferList = new ArrayList<ProblemTransfer>(2);
				for (User kezhang : kezhangs) {
					ProblemTransfer transfer = new ProblemTransfer();
					transfer.setUserID(user.getId());
					transfer.setTransferedUserID(kezhang.getId());
					transfer.setProcessed(false);
					transfer.setProblemID(problem.getId());
					transfer.setMainTransferUser(true);
					transferList.add(transfer);
				}
				problemDao.addProblemTransfers(transferList);
			} else if ((intRole & (Constants.ROLE_TYPE_FUKEZHANG | Constants.ROLE_TYPE_KEZHANG)) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_KEYUAN);
				
				this.addTransferInManageGroups(session, user, problem, new ArrayList<Long>(), true);
			} else if ((intRole & (Constants.ROLE_TYPE_KEYUAN)) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_KEYUAN);
				
				this.addTransferInManageGroups(session, user, problem, new ArrayList<Long>(), true);
			}
		} else if (processMethod == Constants.PROCESS_METHOD_SENDBACK) {
			session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
			
			if ((intRole & (Constants.ROLE_TYPE_ANQUANZONGJIAN | Constants.ROLE_TYPE_SHEBEIFUBUZHANG 
					| Constants.ROLE_TYPE_SHENGCHANFUBUZHANG | Constants.ROLE_TYPE_BUZHANG)) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(),
						Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG);
	
				List<User> informedUserList = equipmentDao.getEquipmentMappingUser(problem.getEquipmentID());
				List<Long> informedUserIDList = new ArrayList<Long>();
				for (User informedUser : informedUserList) {
					informedUserIDList.add(informedUser.getId());
				}
	
				List<Long> zuzhangList = problemDao.getTransferRoleUserByTransferedUserID(problemID,
						Constants.ROLE_TYPE_ZUZHANG);
				for (Long zuzhangID : zuzhangList) {
					if (!user.getId().equals(zuzhangID)) {
						informedUserIDList.add(zuzhangID);
					}
				}
	
				this.addTransferInManageGroups(session, user, problem, informedUserIDList, true);
			} else if ((intRole & (Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG)) > 0) {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(), Constants.PROBLEM_STATUS_BUZHANG);
				
				List<Long> informedUserIDList = new ArrayList<Long>();
				List<Long> buzhangList = problemDao.getTransferRoleUserByTransferedUserID(problemID, Constants.ROLE_TYPE_BUZHANG);
				informedUserIDList.addAll(buzhangList);
				
				this.addTransferInManageGroups(session, user, problem, informedUserIDList, true);
			}
		} else if (processMethod == Constants.PROCESS_METHOD_ESCALATE) {
			int nextStatus = 0;
			if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_BUZHANG) {
				nextStatus = Constants.PROBLEM_STATUS_FUCHUZHANG;
			} else {
				nextStatus = problem.nextStatus(1);
			}
			if (problem.getProcessStatus() != Constants.PROBLEM_STATUS_BUZHANG) {
				User escalateUser = (User) session.getAttribute(Constants.SESSION_ESCALATE_USER);
				if (escalateUser == null) {
					request.setAttribute(Constants.REQUEST_ATTRIBUTE_ERROR, Constants.ERROR_KEY_ESCALATE_USER_ACQUIRED);
					return false;
				}
				
				problemDao.processProblem(feedback, transferID, problemID, user.getId(), nextStatus);
				
				List<ProblemTransfer> transferList = new ArrayList<ProblemTransfer>(1);
				ProblemTransfer transfer = new ProblemTransfer();
				transfer.setUserID(user.getId());
				transfer.setTransferedUserID(escalateUser.getId());
				transfer.setProcessed(false);
				transfer.setProblemID(problem.getId());
				transfer.setMainTransferUser(true);
				transferList.add(transfer);
				problemDao.addProblemTransfers(transferList);
			} else {
				problemDao.processProblem(feedback, transferID, problemID, user.getId(), nextStatus);
				
				Department1 depart1 = (Department1) session.getAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);
				List<User> chuzhangs = departmentDao.getUserIDsByDepartment1IDandRoleType(depart1.getId(), Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG);
				
				List<Long> informedUserIDList = new ArrayList<Long>();
				for (User chuzhang : chuzhangs) {
					informedUserIDList.add(chuzhang.getId());
				}
				
				this.addTransferInManageGroups(session, user, problem, informedUserIDList, true);
			}
		} else if (processMethod == Constants.PROCESS_METHOD_OTHERDEPART) {
			problemDao.processProblem(feedback, transferID, problemID, user.getId(), Constants.PROBLEM_STATUS_FUCHUZHANG);
			
			Department1 depart1 = (Department1) session.getAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);
			List<User> chuzhangs = departmentDao.getUserIDsByDepartment1IDandRoleType(depart1.getId(), Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG);
			
			List<Long> informedUserIDList = new ArrayList<Long>();
			for (User chuzhang : chuzhangs) {
				informedUserIDList.add(chuzhang.getId());
			}
			
			this.addTransferInManageGroups(session, user, problem, informedUserIDList, true);
		}

		return true;
	}

	private void addTransferInManageGroups(HttpSession session, User user, Problem problem,
			List<Long> informedUserIDList, boolean isMainProcessor) {
		Map<Long, Set<Long>> map = (Map<Long, Set<Long>>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_USER);
		if (map != null) {
			Set<Entry<Long, Set<Long>>> entrySet = map.entrySet();
			for (Entry<Long, Set<Long>> entry : entrySet) {
				Set<Long> idSet = entry.getValue();
				for (Long userID : idSet) {
					informedUserIDList.add(userID);
				}
			}
		}
		List<ProblemTransfer> transferList = new ArrayList<ProblemTransfer>();
		for (Long informedUserID : informedUserIDList) {
			ProblemTransfer transfer = new ProblemTransfer();
			transfer.setUserID(user.getId());
			transfer.setTransferedUserID(informedUserID);
			transfer.setProcessed(false);
			transfer.setProblemID(problem.getId());
			transfer.setMainTransferUser(isMainProcessor);

			transferList.add(transfer);
		}

		ProblemDao problemDao = DaoCreator.createProblemDao();
		problemDao.addProblemTransfers(transferList);
		
		session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
	}
	
	public String getSessionManageGroupUserStr(HttpSession session) {
		StringBuilder sb = new StringBuilder();
		UserDao userDao = DaoCreator.createUserDao();
		
		Map<Long, Set<Long>> map = (Map<Long, Set<Long>>) session.getAttribute(Constants.SESSION_MANAGE_GROUP_USER);
		if (map != null) {
			Set<Entry<Long, Set<Long>>> entrySet = map.entrySet();
			for (Entry<Long, Set<Long>> entry : entrySet) {
				Set<Long> idSet = entry.getValue();
				for (Long userID : idSet) {
					User user = userDao.getUserByID(userID);
					if (sb.length() == 0) {
						sb.append(user.getName());
					} else {
						sb.append(" " + user.getName());
					}
				}
			}
		}
		
		return sb.toString();
	}

	private boolean hasRightToFeedback(Problem problem, User user, Long transferID, JobPost post1, JobPost post2,
			JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);
		
		if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG) {
			if ((intRole & (Constants.ROLE_TYPE_ZUZHANG | Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_ZUYUAN)) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_ANQUANZONGJIAN_FUBUZHANG) {
			if ((intRole & (Constants.ROLE_TYPE_ANQUANZONGJIAN | Constants.ROLE_TYPE_SHEBEIFUBUZHANG | Constants.ROLE_TYPE_SHENGCHANFUBUZHANG)) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_BUZHANG) {
			if ((intRole & Constants.ROLE_TYPE_BUZHANG) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_ZUYUAN) {
			if ((intRole & Constants.ROLE_TYPE_ZUYUAN) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_CHUZHANG) {
			if ((intRole & Constants.ROLE_TYPE_CHUZHANG) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_FUCHUZHANG) {
			if ((intRole & (Constants.ROLE_TYPE_CHUZHANG | Constants.ROLE_TYPE_FUCHUZHANG)) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_KEZHANG) {
			if ((intRole & Constants.ROLE_TYPE_KEZHANG) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_FUKEZHANG) {
			if ((intRole & (Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG)) == 0) {
				return false;
			}
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_KEYUAN) {
			if ((intRole & Constants.ROLE_TYPE_KEYUAN) == 0) {
				return false;
			}
		}

		return true;
	}

	public boolean isShowShengChanManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if ((user.getDepartment1ID() >= Constants.SHIYEBU_ID_HUANJINGJIANCE && user.getDepartment1ID() <= Constants.SHIYEBU_ID_LIANYOUYITIXIANGMU)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_CHUYUN1)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_HUAGONG1)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_HUAGONG2)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_HUAGONG3)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_HUAGONG4)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_LIANYOU1)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_LIANYOU2)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_LIANYOU3)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_LIANYOU4)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_LIANYOU5)
				|| user.getDepartment1ID().equals(Constants.SHIYEBU_ID_SHUIWUGUANLI)) {
			return true;
		}

		return false;
	}

	public boolean isShowShengchanShebeiManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user.getDepartment1ID().equals(Constants.SHIYEBU_ID_CHUYUN2)) {
			return true;
		}

		return false;
	}

	public boolean isShowJishuManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user.getDepartment1ID().equals(Constants.SHIYEBU_ID_CHANPINFENXIFUWU)) {
			return true;
		}

		return false;
	}

	public boolean isShowKeyanJishuFuwuManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user.getDepartment1ID().equals(Constants.SHIYEBU_ID_CHANPINFENXIFUWU)) {
			return true;
		}

		return false;
	}

	public boolean isShowRedianYunxingManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user.getDepartment1ID().equals(Constants.SHIYEBU_ID_DONGLIGUANLI)) {
			return true;
		}

		return false;
	}

	public boolean isShowGongyongGongchengManageGroup(HttpServletRequest request) {
		HttpSession session = request.getSession();

		User user = (User) session.getAttribute(Constants.SESSION_USER);
		if (user.getDepartment1ID().equals(Constants.SHIYEBU_ID_DONGLIGUANLI)) {
			return true;
		}

		return false;
	}

	public List<User> getEscalateUserList(HttpServletRequest request) {
		String problemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		Long problemID = 0L;
		try {
			problemID = new Long(problemIDStr);
		} catch (Exception e) {
		}

		HttpSession session = request.getSession();
		List<User> escalateUsers = new ArrayList<User>();
		User user = (User) session.getAttribute(Constants.SESSION_USER);

		UserDao userDao = DaoCreator.createUserDao();
		ProblemDao problemDao = DaoCreator.createProblemDao();
		Problem problem = problemDao.getProblemByID(problemID);
		if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_ZUZHANG_ZHUANGZHIZHANG) {
			List<User> users1 = userDao.getUsersByRoleType(user.getDepartment1ID(), Constants.ROLE_TYPE_ANQUANZONGJIAN);
			List<User> users2 = userDao
					.getUsersByRoleType(user.getDepartment1ID(), Constants.ROLE_TYPE_SHEBEIFUBUZHANG);
			List<User> users3 = userDao.getUsersByRoleType(user.getDepartment1ID(),
					Constants.ROLE_TYPE_SHENGCHANFUBUZHANG);

			escalateUsers.addAll(users1);
			escalateUsers.addAll(users2);
			escalateUsers.addAll(users3);
		} else if (problem.getProcessStatus() == Constants.PROBLEM_STATUS_ANQUANZONGJIAN_FUBUZHANG) {
			List<User> users = userDao.getUsersByRoleType(user.getDepartment1ID(), Constants.ROLE_TYPE_BUZHANG);

			escalateUsers.addAll(users);
		}

		return escalateUsers;
	}

	public boolean chooseEscalateUser(HttpServletRequest request) {
		String idStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
		Long id = 0L;
		try {
			id = new Long(idStr);
		} catch (Exception e) {
		}

		HttpSession session = request.getSession();

		UserDao userDao = DaoCreator.createUserDao();
		User user = userDao.getUserByID(id);

		session.setAttribute(Constants.SESSION_ESCALATE_USER, user);

		return true;
	}

	public boolean showYiZhengGaiButton(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);

		if ((intRole & Constants.ROLE_TYPE_ZUYUAN) > 0 || (intRole & Constants.ROLE_TYPE_KEYUAN) > 0) {
			return false;
		}

		return true;
	}

	public boolean showTuihuiButton(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);

		if ((intRole & Constants.ROLE_TYPE_ANQUANZONGJIAN) > 0 || (intRole & Constants.ROLE_TYPE_SHEBEIFUBUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_SHENGCHANFUBUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_FUCHUZHANG) > 0 || (intRole & Constants.ROLE_TYPE_CHUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_BUZHANG) > 0) {
			return true;
		}

		return false;
	}

	public boolean showXiangGuanChuShiButton(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);

		if ((intRole & Constants.ROLE_TYPE_FUKEZHANG) > 0 || (intRole & Constants.ROLE_TYPE_FUCHUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_KEZHANG) > 0 || (intRole & Constants.ROLE_TYPE_CHUZHANG) > 0) {
			return true;
		}

		return false;
	}

	public boolean showShangBaoZhiNengChuShiButton(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);

		if ((intRole & Constants.ROLE_TYPE_ZHUANGZHIZHANG) > 0 || (intRole & Constants.ROLE_TYPE_ZUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_SHEBEIFUBUZHANG) > 0 || (intRole & Constants.ROLE_TYPE_SHENGCHANFUBUZHANG) > 0
				|| (intRole & Constants.ROLE_TYPE_BUZHANG) > 0 || (intRole & Constants.ROLE_TYPE_ANQUANZONGJIAN) > 0) {
			return true;
		}

		return false;
	}
	
	public boolean showZhuanJiaoTaoRenButton(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = this.calRole(post1, post2, post3);
		
		if ((intRole & (Constants.ROLE_TYPE_ZUZHANG | Constants.ROLE_TYPE_ZHUANGZHIZHANG | Constants.ROLE_TYPE_KEYUAN
				| Constants.ROLE_TYPE_KEZHANG | Constants.ROLE_TYPE_FUKEZHANG | Constants.ROLE_TYPE_FUCHUZHANG 
				| Constants.ROLE_TYPE_CHUZHANG)) > 0) {
			return true;
		}
		
		return false;
	}

	public int calRole(JobPost post1, JobPost post2, JobPost post3) {
		int intRole = 0;
		if (post1 != null) {
			intRole |= post1.getRoleType();
		}
		if (post2 != null) {
			intRole |= post2.getRoleType();
		}
		if (post3 != null) {
			intRole |= post3.getRoleType();
		}

		return intRole;
	}
}
